#include"SearchSpec.h"
SearchSpec::SearchSpec()
{
	must_str="";
}
void SearchSpec::read_in()
{
	char temp;
	string totalOr,totalNone,totalAnd,check;//used for getline to get the words for OR/NONE/AND
	cout<<"\nMust include exact of this string"<<endl;
	temp=cin.get();
	getline(cin,must_str);
	if(temp!='\n')
	must_str=temp+must_str;
	must_str=convert_lower(must_str);

	cout<<"\nMust include all of these words(seperated by "" AND "",example: ""apple AND star"")"<<endl;
	getline(cin,totalAnd);
	//cout<<"totalAND is "<<totalAnd<<endl;
	cout<<"\nAny of these words(seperated by "" OR "", example:""dog OR apple"")"<<endl;
	getline(cin,totalOr);
	//cout<<"totalOR is "<<totalOr<<endl;

	cout<<"\nNone of these words(with ""- ""(space) in front, example:""- dog - apple"")"<<endl;
	getline(cin,totalNone);
	//cout<<"total NONE is "<<totalNone<<endl;

	istringstream in(totalOr);
	while(in.good())
	{
		in>>check;
		if(check!="OR"&&check!="")//Should not take in empty requirement
			or_str.push_back(convert_lower(check));
	}

	check.clear();//important! otherwise will read in the last word from the previous stringstream
	istringstream in2(totalNone);
	while(in2.good())
	{
		in2>>check;
		if(check!="-"&&check!="")
			none_str.push_back(convert_lower(check));
	}
	check.clear();
	istringstream in3(totalAnd);
	while(in3.good())
	{
		in3>>check;
		if(check!="AND"&&check!="")
			and_str.push_back(convert_lower(check));
	}
	/*cout<<"OR strings:"<<endl;
	for(int i=0;i<or_str.size();i++)
		cout<<or_str[i]<<" ";
	cout<<endl;

	cout<<"AND strings:"<<endl;
	for(int i=0;i<and_str.size();i++)
		cout<<and_str[i]<<" ";
	cout<<endl;
	cout<<"NONE strings:"<<endl;
	for(int i=0;i<none_str.size();i++)
		cout<<none_str[i]<<" ";
	cout<<endl;*/
}
	bool SearchSpec::is_empty()
	{
		if(must_str==""&&or_str.empty()&&none_str.empty()&&and_str.empty())
		{
		return true;
		}
		return false;
	}
	bool SearchSpec::match(string target)
	{
		int i;
		//check must have string
		if(target.find(must_str)==string::npos)
			return false;
		//check OR strings
		if(or_str.size()!=0)
		{
		for(i=0;i<or_str.size();i++)
		{
			if(target.find(or_str[i])!=string::npos)
				break;
		}
		if(i==or_str.size())
			return false;
		}
		//check None strings
		if(none_str.size()!=0)
		{
		for(i=0;i<none_str.size();i++)
		{
			if(target.find(none_str[i])!=string::npos)
				return false;
		}
		}
		//check AND strings
		if(and_str.size()!=0)
		{
		for(i=0;i<and_str.size();i++)
		{
			if(target.find(and_str[i])==string::npos)
				return false;
		}
		}
		return true;
	}	
	
	string SearchSpec:: convert_lower(string str)
{
	for(int i = 0;i < str.size();i ++)
		str[i]=tolower(str[i]);
	return str;
}